package com.lbj.googleplay.googleplay100.http.protocol;

import com.lbj.googleplay.googleplay100.domain.AppInfo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

/**
 * 首页详情页的网络数据解析
 * 
 * @author Kevin
 * @date 2015-10-28
 */
public class HomeDetailProtocol extends BaseProtocol<AppInfo> {

	private  String packageName;

	private ArrayList<String> pictures;

	public HomeDetailProtocol(String packageName) {

		this.packageName = packageName;
	}

	@Override
	public String getKey() {
		return "detail";
	}

	@Override
	public String getParams() {
		return "&packageName="+packageName;// 如果没有参数,就传空串,不要传null
	}

	@Override
	public AppInfo parseData(String result) {

        try {
            JSONObject jo = new JSONObject(result);

            AppInfo info = new AppInfo();
            info.des = jo.getString("des");
            info.downloadUrl = jo.getString("downloadUrl");
            info.iconUrl = jo.getString("iconUrl");
            info.id = jo.getString("id");
            info.name = jo.getString("name");
            info.packageName = jo.getString("packageName");
            info.size = jo.getLong("size");
            info.stars = (float) jo.getDouble("stars");

            info.author = jo.getString("author");
            info.date = jo.getString("date");
            info.downloadNum = jo.getString("downloadNum");
            info.version = jo.getString("version");

            //解析安全信息
            JSONArray ja = jo.getJSONArray("safe");
            ArrayList<AppInfo.SafeInfo> safeInfos = new ArrayList<>();

            for (int i = 0; i < ja.length(); i++) {
                JSONObject jo1 = ja.getJSONObject(i);
                AppInfo.SafeInfo safeInfo = new AppInfo.SafeInfo();
                safeInfo.safeDes = jo1.getString("safeDes");
                safeInfo.safeDesUrl = jo1.getString("safeDesUrl");
                safeInfo.safeUrl = jo1.getString("safeUrl");
                safeInfos.add(safeInfo);
            }
            info.safe = safeInfos;

            //解析截图信息
            JSONArray ja1 = jo.getJSONArray("screen");
            ArrayList<String> screen = new ArrayList<>();

            for (int i = 0; i <ja1.length() ; i++) {
                String pic = ja1.getString(i);
                screen.add(pic);
            }
            info.screen = screen;

            return info;
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
	}

	public ArrayList<String> getPictureList(){
		return pictures;
	}
}
